library(tidyverse)
library(stationaRy)
library(sf)
library(tmap)
library(spData)
library(AOI)
library(climateR)
library(raster)
library(rasterVis)
library(patchwork)Day 1: A few examples
A few interesting examples
Precip and temp for specified location (point)
# Get the data
#loc2use <- 'Death Valley National Park'
loc2use <- 'Union College'
AOI = AOI::geocode(loc2use,
pt = TRUE)
ts = getGridMET(AOI, varname = c("tmmx","tmmn", "pr"),
startDate = "2025-01-01",
endDate = "2025-12-30")# convert units
ts <- ts %>%
mutate(tmax = ((tmmx - 273.15)*(9/5) +32),
tmin = ((tmmn - 273.15)*(9/5) +32),
prcp = pr/25.4
)# Summary table
ts %>%
summarize(pcrp_max = max(prcp),
temp_max = max(tmax),
temp_min = min(tmin),
temp_mean = mean((tmin+tmax)/2),
temp_max_change = max(tmax - tmin),
n_days = n()
)# Make the figures
fig_01 <- ts %>%
ggplot() +
geom_line(aes(x = date, y = tmax), color = "red") +
geom_line(aes(x = date, y = tmin), color = "blue") +
labs(x = "",
y = "Temperature (F)"
) +
geom_hline(yintercept = 32, linetype = "dashed") +
theme_bw()
fig_02 <- ts %>%
ggplot() +
geom_col(aes(x = date, y = prcp), color = "black") +
labs(y = "Precipitation (inches)") +
theme_bw()# Make the figures
(fig_01 / fig_02) +
plot_annotation(title = paste(loc2use,": Temperature and precipitation", sep = ""),
caption = "Data source: GridMET",
tag_levels = "a"
) 
Obtain and analyze precipitation data for Hurricane Helene (2024)
hurricane_data = getGridMET(aoi_get(state = c("NC")),
varname = "pr",
startDate = "2024-09-23", endDate = "2024-09-28")r = terra::rast(hurricane_data)r_inches <- r/25.4Static map
tmap_mode("plot")
fig_map <- r_inches %>%
tm_shape() +
tm_raster(style = "cont") +
tm_shape(spData::us_states) +
tm_borders()
fig_map
Interactive map
tmap_mode("view")
fig_map tmap_mode("plot")Get the maximum precipitation amount for each day of the storm
round(terra::global(x = r_inches, "max", na.rm = T),2) %>% as.data.frame()Get climate data for specified region of the US
sf::sf_use_s2(FALSE)Spherical geometry (s2) switched off
temperature_US = getGridMET(aoi_get(state = "conus"),
varname = "tmmn",
startDate = "2025-12-19", endDate = "2025-12-19")
#> Spherical geometry (s2) switched off
#> Spherical geometry (s2) switched on
#> startDate = "2024-10-31", endDate = "2024-10-31"temperature_US <- terra::rast(temperature_US)temperature_US <- temperature_US - 273.15temperature_US <- (temperature_US*9/5) + 32tmap_mode("plot")tmap mode set to plotting
temperature_US %>%
tm_shape() +
tm_raster(style = "cont", palette = "-RdBu", midpoint = 32, title = "Min Temp (C)") +
tm_shape(spData::us_states) +
tm_borders() +
tm_layout(legend.outside.position = "right", legend.outside = T)
Map of springs in New Mexico
library(osmdata)
library(tigris)
library(sf)
#library(osmplotr)
library(tmaptools)
library(OpenStreetMap)loc2use <- "New Mexico"
bb_values <- getbb(loc2use)
bb_values min max
x -109.0502 -103.00223
y 31.3322 37.00018
springs_data <- opq(bb_values) %>%
add_osm_feature(key = 'natural', value = 'spring') %>%
osmdata_sf()loc_border <- spData::us_states %>%
filter(NAME == "New Mexico")tmap_mode("view")tmap mode set to interactive viewing
map_springs <-
tm_shape(loc_border) +
tm_borders(col = "black") +
tm_shape(springs_data$osm_points) +
tm_dots(col = "blue") map_springsObtain streamflow data from the USGS (Colorado River at Lee’s Ferry example)
library(dataRetrieval)
library(lubridate)df_stream_data <- readNWISdv(siteNumbers = "09380000",
parameterCd = c("00060"),
statCd = "00003") %>%
renameNWISColumns()GET: https://waterservices.usgs.gov/nwis/dv/?site=09380000&format=waterml,1.1&ParameterCd=00060&StatCd=00003&startDT=1851-01-01
Daily flows
df_stream_data %>%
ggplot(aes(x = Date, y = Flow)) +
geom_line() +
theme_classic()
Annual flow statistics
table_flows <- df_stream_data %>%
mutate(Year = year(Date)) %>%
group_by(Year) %>%
summarize(mean_flow = mean(Flow, na.rm= T),
min_flow = min(Flow, na.rm = T),
max_flow = max(Flow, na.rm = T),
n_meas = n()) %>%
filter(n_meas > 350)
table_flowsfig_max <- table_flows %>%
ggplot(aes(x = Year)) +
geom_line(aes(y = max_flow), size = 1, color = "blue") +
#geom_line(aes(y = min_flow), size = 1, color = "red") +
#geom_line(aes(y = mean_flow), size = 1, color = "black") +
theme_classic()Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
fig_min <- table_flows %>%
ggplot(aes(x = Year)) +
geom_line(aes(y = min_flow), size = 1, color = "red") +
#geom_line(aes(y = mean_flow), size = 1, color = "black") +
theme_classic()
fig_mean <- table_flows %>%
ggplot(aes(x = Year)) +
geom_line(aes(y = mean_flow), size = 1, color = "black") +
theme_classic()fig_mean/fig_max/fig_min
Obtain US Census data
https://api.census.gov/data/2015/acs/acs5/variables
library(tidycensus)
Attaching package: 'tidycensus'
The following object is masked from 'package:tigris':
fips_codes
census_example_df <- get_acs(geography = "county",
variables = c(medincome = "B19013_001"),
state = "NY",
year = 2021)Getting data from the 2017-2021 5-year ACS
census_example_df %>%
arrange(-estimate)census_example_df %>%
mutate(NAME = gsub(" County, New York", "", NAME)) %>%
ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
geom_errorbarh(aes(xmin = estimate - moe, xmax = estimate + moe)) +
geom_point(color = "red", size = 3) +
labs(title = "Household income by county in New York",
subtitle = "2021 American Community Survey",
y = "",
x = "ACS estimate (bars represent margin of error)") +
theme_bw()
tmap_mode("plot")tmap mode set to plotting
map_income <- tm_shape(Schdy) +
tm_polygons(col = "estimate", alpha = 1)
map_income
map_income_interactive <- tm_shape(Schdy) +
tm_polygons(col = "estimate", alpha = 0.6)tmap_mode("view")tmap mode set to interactive viewing
map_income_interactive